﻿using UnityEngine;
using UnityEditor;
using System.Security.Cryptography;

public class ConfigHelper
{
    static string ms_path = "Assets/Config/config.asset";

    public static ConfigData GetData()
    {
        return AssetDatabase.LoadAssetAtPath<ConfigData>(ms_path);
    }

    delegate void SetDataHandler(ConfigData data);
    static void SaveData(SetDataHandler handler)
    {
        bool needCreate = false;

        ConfigData data = AssetDatabase.LoadAssetAtPath<ConfigData>(ms_path);
        if (data == null)
        {
            data = ScriptableObject.CreateInstance<ConfigData>();
            needCreate = true;
        }

        handler(data);

        if (needCreate)
        {
            AssetDatabase.CreateAsset(data, ms_path);
        }
        else
        {
            EditorUtility.SetDirty(data);
            AssetDatabase.SaveAssets();
        }
        AssetDatabase.Refresh();
    }

    // --------------------------------------------------------------------------------------------
    public static string ProjectName
    {
        get
        {
            ConfigData data = GetData();
            return data.ProjectName;
        }

        set
        {
            SaveData((data) => {
                data.ProjectName = value;
            });
        }
    }

    // 游戏名
    public static string GameName
    {
        get
        {
            ConfigData data = GetData();
            return data.GameName;
        }
    }

    public static bool ResourceSplit
    {
        get
        {
            ConfigData data = GetData();
            return data.ResourceSplit;
        }

        set
        {
            SaveData((data) => {
                data.ResourceSplit = value;
            });
        }
    }

    // 版本号
    public static string Version
    {
        get
        {
            ConfigData data = GetData();
            return data.Version;
        }

        set
        {
            SaveData((data) => {
                data.Version = value;
            });
        }
    }

    public static string UpdateName
    {
        get
        {
            ConfigData data = GetData();
            return data.UpdateName;
        }
    }

    public static string HDResName
    {
        get
        {
            ConfigData data = GetData();
            return data.HDResName;
        }
    }

    public static string UpdateURL
    {
        get
        {
            ConfigData data = GetData();
            return data.UpdateURL;
        }
    }

    public static string UpdateDataURL
    {
        get
        {
            ConfigData data = GetData();
            return data.UpdateDataURL;
        }
    }

    public static void UpdatePackageCode()
    {
        SaveData((data) => {
            //data.PackageCode = Extends.GetTimeStamp();
        });
    }

    public static void CalcNeedDiskSize()
    {
        SaveData((data) => {
            //long size = FileHelper.GetDirectorySize(ExportAssetBundle.AssetBundlePath);
            //data.MinNeedDiskSize = (long)(size * 1.1);
        });
    }

    public static bool UseLuaJit
    {
        get
        {
            ConfigData data = GetData();
            return data.UseLuaJit;
        }

        set
        {
            SaveData((data) => {
                data.UseLuaJit = value;
            });
        }
    }

    // 检测更新
    public static bool CheckUpdate
    {
        get
        {
            ConfigData data = GetData();
            return data.CheckUpdate;
        }

        set
        {
            SaveData((data) => {
                data.CheckUpdate = value;
            });
        }
    }

    // 大图模式
    public static bool UseTexturePacker
    {
        get
        {
            ConfigData data = GetData();
            return data.UseTexturePacker;
        }

        set
        {
            SaveData((data) => {
                data.UseTexturePacker = value;
            });
        }
    }

    public static ReleaseType Release
    {
        get
        {
            ConfigData data = GetData();
            return data.Release;
        }

        set
        {
            SaveData((data) => {
                data.Release = value;
            });
        }
    }

    public static string BundleId
    {
        get
        {
            ConfigData data = GetData();
            return data.BundleId;
        }

        set
        {
            SaveData((data) => {
                data.BundleId = value;
            });
        }
    }

    public static bool IsDeveloper
    {
        get
        {
            ConfigData data = GetData();
            return data.IsDeveloper;
        }
    }

    // --------------------------------------------------------------------------------------------
    // 密钥
    public static void GenerateKey()
    {
        RijndaelManaged manager = new RijndaelManaged();
        manager.GenerateKey();
        manager.GenerateIV();

        SaveData((data) => {
            data.Key = manager.Key;
            data.IV = manager.IV;
        });
    }
}